<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property integer $id
 * @property string $parent_id
 * @property string $title
 * @property string $descriptions
 * @property string $status
 * @property string $type_position
 * @property string $warranty
 * @property string $balance
 * @property integer $article
 */
class Items extends CActiveRecord
{
	const STATUS_ON_SITE  = 1; // на сайті
	const STATUS_OFF_SITE = 0; // не відображати на сайті
	
	const STATUS_TYPE_POSITION_FOLDER  = 1; //каталог
	const STATUS_TYPE_POSITION_ITEM	   = 0;// товар
	
	const COUNT_VIEW_ITTEMS  = 12; // на сайті
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'c_items';
	}

	/**
	 * повернути масив статусів (На сайты чи ны) 
	 * @return type
	 */
	public static function get_array_statuses_all(){
		return array(self::STATUS_ON_SITE, self::STATUS_OFF_SITE);
	}

	
	/**
	 * повернути масив статусів 
	 * @return type
	 */
	public static function get_assoc_array_statuses_all(){
	    $x = self::get_array_statuses_all();
	    $x[self::STATUS_ON_SITE] = Yii::t('main', 'Не опубліковано');
	    $x[self::STATUS_OFF_SITE] = Yii::t('main', 'Не опубліковано');
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status', 'numerical', 'integerOnly'=>true),
			array('date, name', 'required'),
			array('date', 'type', 'type' => 'date', 'message' => '{attribute}:  не правильний формат дати!', 'dateFormat' => 'yyyy-MM-dd'),
			array('name, short, title, keywords, descriptions', 'length', 'max'=>250),
			array('text', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, short, text, date, title, keywords, descriptions, status', 'safe', 'on'=>'search'),
		);
	}

	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'parent_id' => 'parent_id',
			'title' => 'title',
			'descriptions' => 'descriptions',
			'status' => 'status',
			'type_position' => 'type_position',
			'warranty' => 'warranty',
			'balance' => 'balance',
			'article' => 'article',
		);
	}

	
	public static function get_childrens_categories($parent_id_category){
	    $criteria = new CDbCriteria();
	    $criteria->condition = 'parent_id =:parent_id and type_position =:type_position and status = :status';
	    $criteria->params = array(':parent_id' => $parent_id_category, ':type_position' => self::STATUS_TYPE_POSITION_FOLDER,':status' => self::STATUS_ON_SITE);
	    $criteria->limit = 8;

	    $sub_category = self::model()->findAll($criteria);

	    return $sub_category;
	}
	
	public static function get_array_parernt_category_static(){
	    return array(8,1882,1992,15064);
	}
	
	/**
	 *  рекурсивно повертаю яка вложеність з 0 по ....
	 * @param type $id
	 * @param type $level
	 * @return type
	 */
	public static function get_level_nesting($id,$level = 0){
	    $item = Items::model()->findByPk($id);

	    if(empty($item) || empty($item->parent_id)){
			return $level;
		}

	    $level++;

	    return self::get_level_nesting($item->parent_id, $level);
	}
}